﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
namespace Usas.DAL.Cms
{
    /// <summary>
    /// 数据访问类cmsBorder。
    /// </summary>
    public class cmsBorder
    {
        public cmsBorder()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int BorderId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from cmsBorder");
            strSql.Append(" where BorderId=" + BorderId + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Usas.Model.Cms.cmsBorder model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.TypeId != null)
            {
                strSql1.Append("TypeId,");
                strSql2.Append("" + model.TypeId + ",");
            }
            if (model.BorderOrder != null)
            {
                strSql1.Append("BorderOrder,");
                strSql2.Append("" + model.BorderOrder + ",");
            }
            if (model.BorderName != null)
            {
                strSql1.Append("BorderName,");
                strSql2.Append("'" + model.BorderName + "',");
            }
            if (model.ViewPurview != null)
            {
                strSql1.Append("ViewPurview,");
                strSql2.Append("'" + model.ViewPurview + "',");
            }
            if (model.EditPurview != null)
            {
                strSql1.Append("EditPurview,");
                strSql2.Append("'" + model.EditPurview + "',");
            }
            if (model.ImgFile != null)
            {
                strSql1.Append("ImgFile,");
                strSql2.Append("'" + model.ImgFile + "',");
            }
            if (model.Del != null)
            {
                strSql1.Append("Del,");
                strSql2.Append("" + (model.Del ? 1 : 0) + ",");
            }
           
            if (model.IsCheck != null)
            {
                strSql1.Append("IsCheck,");
                strSql2.Append("" + (model.IsCheck ? 1 : 0) + ",");
            }
            if (model.CheckPerson != null)
            {
                strSql1.Append("CheckPerson,");
                strSql2.Append("" + model.CheckPerson + ",");
            }
            strSql.Append("insert into cmsBorder(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Usas.Model.Cms.cmsBorder model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update cmsBorder set ");
            if (model.TypeId != null)
            {
                strSql.Append("TypeId=" + model.TypeId + ",");
            }
            if (model.BorderOrder != null)
            {
                strSql.Append("BorderOrder=" + model.BorderOrder + ",");
            }
            if (model.BorderName != null)
            {
                strSql.Append("BorderName='" + model.BorderName + "',");
            }
            if (model.ViewPurview != null)
            {
                strSql.Append("ViewPurview='" + model.ViewPurview + "',");
            }
            if (model.EditPurview != null)
            {
                strSql.Append("EditPurview='" + model.EditPurview + "',");
            }
            if (model.ImgFile != null)
            {
                strSql.Append("ImgFile='" + model.ImgFile + "',");
            }
            if (model.Del != null)
            {
                strSql.Append("Del=" + (model.Del ? 1 : 0) + ",");
            }
            
            if (model.IsCheck != null)
            {
                strSql.Append("IsCheck=" + (model.IsCheck ? 1 : 0) + ",");
            }
            if (model.CheckPerson != null)
            {
                strSql.Append("CheckPerson=" + model.CheckPerson + ",");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where BorderId=" + model.BorderId + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int BorderId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from cmsBorder ");
            strSql.Append(" where BorderId=" + BorderId + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Usas.Model.Cms.cmsBorder GetModel(int BorderId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" BorderId,TypeId,BorderOrder,BorderName,ViewPurview,EditPurview,ImgFile,Del,IsCheck,CheckPerson ");
            strSql.Append(" from cmsBorder ");
            strSql.Append(" where BorderId=" + BorderId + " ");
            Usas.Model.Cms.cmsBorder model = new Usas.Model.Cms.cmsBorder();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["BorderId"].ToString() != "")
                {
                    model.BorderId = int.Parse(ds.Tables[0].Rows[0]["BorderId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TypeId"].ToString() != "")
                {
                    model.TypeId = int.Parse(ds.Tables[0].Rows[0]["TypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BorderOrder"].ToString() != "")
                {
                    model.BorderOrder = int.Parse(ds.Tables[0].Rows[0]["BorderOrder"].ToString());
                }
                model.BorderName = ds.Tables[0].Rows[0]["BorderName"].ToString();
                model.ViewPurview = ds.Tables[0].Rows[0]["ViewPurview"].ToString();
                model.EditPurview = ds.Tables[0].Rows[0]["EditPurview"].ToString();
                model.ImgFile = ds.Tables[0].Rows[0]["ImgFile"].ToString();
                if (ds.Tables[0].Rows[0]["Del"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["Del"].ToString() == "1") || (ds.Tables[0].Rows[0]["Del"].ToString().ToLower() == "true"))
                    {
                        model.Del = true;
                    }
                    else
                    {
                        model.Del = false;
                    }
                }
             
                if (ds.Tables[0].Rows[0]["IsCheck"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsCheck"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsCheck"].ToString().ToLower() == "true"))
                    {
                        model.IsCheck = true;
                    }
                    else
                    {
                        model.IsCheck = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["CheckPerson"].ToString() != "")
                {
                    model.CheckPerson = int.Parse(ds.Tables[0].Rows[0]["CheckPerson"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BorderId,TypeId,BorderOrder,BorderName,ViewPurview,EditPurview,ImgFile,Del,IsCheck,CheckPerson ");
            strSql.Append(" FROM cmsBorder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" BorderId,TypeId,BorderOrder,BorderName,ViewPurview,EditPurview,ImgFile,Del,IsCheck,CheckPerson ");
            strSql.Append(" FROM cmsBorder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */

        #endregion  成员方法
    }
}

